//
//  SY_Payment_WebViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 2016/12/21.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Payment_WebViewController.h"
#import <cmbkeyboard/CMBWebKeyboard.h>
#import <cmbkeyboard/NSString+Additions.h>
#import <WebKit/WebKit.h>

@interface SY_Payment_WebViewController ()<WKUIDelegate,UIGestureRecognizerDelegate,UIWebViewDelegate>
{
    UIBarButtonItem *backItem;
}

@property (nonatomic, strong) UIWebView *myWebView;

@end


@implementation SY_Payment_WebViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    
    //[self reloadWebView];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    
}

#pragma mark - dealloc
- (void)dealloc
{
    [[CMBWebKeyboard shareInstance] hideKeyboard];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back2"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    _myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
    _myWebView.delegate = self;
    _myWebView.backgroundColor = [UIColor clearColor];
    _myWebView.scalesPageToFit =YES;
    
    [self.view addSubview:_myWebView];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_receipt_ywt" forKey:@"method"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:[NSString stringWithFormat:@"%ld",self.detailModel.payid] forKey:@"payid"];
    [param setObject:@"2" forKey:@"typeid"];
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        
        NSDictionary *dict = [jsonObject objectForKey:@"re_data"];
        
        NSURL *url = [NSURL URLWithString: @"https://netpay.cmbchina.com/netpayment/BaseHttp.dll?MB_EUserPay"];
        NSString *body = [NSString stringWithFormat: @"jsonRequestData=%@", [dict objectForKey:@"paystr"]];
        
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
        [request setHTTPMethod: @"POST"];
        [request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
        [self->_myWebView loadRequest: request];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -返回上一层控制器
-(void)back:(id)sender{
    
    if ([_myWebView canGoBack]) {
        
        NSMutableDictionary *parmaDic = [[NSMutableDictionary alloc]init];
        
        [parmaDic setObject:@"api_receiptinfo_toparent" forKey:@"method"];
        [parmaDic setObject:USERID forKey:@"userid"];
        [parmaDic setObject:[NSString stringWithFormat:@"%ld",self.detailModel.payid] forKey:@"payid"];
        
        [Utils get:nil parameters:parmaDic success:^(NSInteger recode, id jsonObject) {
            
            NSArray *dataArr = [jsonObject objectForKey:@"re_data"];
            
            if (dataArr != nil && dataArr.count > 0) {
                NSDictionary *detailDic = [[NSDictionary alloc]init];
                detailDic = [dataArr objectAtIndex:0];
                self.detailModel = [SY_Par_PaymentModel mj_objectWithKeyValues:detailDic];
                
            }
            
            if (self.detailModel.state != 1) {
                [[NSNotificationCenter defaultCenter]postNotificationName:@"SY_PAYMENTLIST_REFRESH" object:self.detailModel];
                
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
            }else {
                [self->_myWebView goBack];
            }
            
        } failure:^(NSInteger recode, NSError *error) {
            
        }];
        
        
    }else {
        
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    }
}

static BOOL FROM = FALSE;

- (BOOL)webView:(UIWebView *)_sywebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    if ([request.URL.host isCaseInsensitiveEqualToString:@"cmbls"]) {
        CMBWebKeyboard *secKeyboard = [CMBWebKeyboard shareInstance];
        [secKeyboard showKeyboardWithRequest:request];
        secKeyboard.webView = _sywebView;
        
        UITapGestureRecognizer* myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.view addGestureRecognizer:myTap]; //这个可以加到任何控件上,比如你只想响应WebView，我正好填满整个屏幕
        myTap.delegate = self;
        myTap.cancelsTouchesInView = NO;
        return NO;
    }
    
    if ([[NSString stringWithFormat:@"%@",request.URL] hasPrefix:@"http://api.our100.net/Server/?YwtPayOK=OK"]) {
        
        self.detailModel.state = 2;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SY_PAYMENTLIST_REFRESH" object:self.detailModel];
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
        
        return NO;
        
    }
    
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Load webView error:%@", [error localizedDescription]);
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    if (FROM) {
        
        return;
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
}

@end
